﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Users_UpdateHappyPet : BasePage
{
    HapppNetBLL bll = new HapppNetBLL();
    protected int TempleId = 0;
    protected AspNetCs.Bll.Temple templateBLL = AspNetCs.Bll.TempleBll.CreateNew;

    protected string notSet = "display:none;";
    protected string play = "display:block;";
    protected string musicPath = "";
    protected int id = 0;
    protected int MusicId = 0;
    protected DataTable oldDataTable = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx", "请您登录", true);
        if (!IsPostBack)
        {
            string Id = Request["id"];
            ViewState["id"] = Id;
            if (string.IsNullOrEmpty(Id))
            {
                ResultStr("Login.aspx", "参数错误");
            }
            else
            {
                id = StringHelper.isNumber_int(Id);
                ViewState["HappyPetInfo"] = bll.getHappyPetById(" * ", StringHelper.isNumber_int(Id));
                InitPage();
            }
            MusicId = StringHelper.isNumber_int(oldDataTable.Rows[0]["MusicId"].ToString());
        }
        else
        {
            if (ViewState["HappyPetInfo"] != null)
            {
                oldDataTable = ViewState["HappyPetInfo"] as DataTable;
            }
            id = StringHelper.isNumber_int(ViewState["id"].ToString());
            MusicId = StringHelper.isNumber_int(oldDataTable.Rows[0]["MusicId"].ToString());
        }
        initMusic();
    }


    public void InitPage()
    {

        if (ViewState["HappyPetInfo"] != null)
        {
            oldDataTable = ViewState["HappyPetInfo"] as DataTable;
        }
        if (oldDataTable.Rows.Count > 0)
        {
            txtPetName.Text = oldDataTable.Rows[0]["PetName"].ToString();
            Txt_UpImg.Text = oldDataTable.Rows[0]["PetPhoto"].ToString();
            WebEditor1.Text = oldDataTable.Rows[0]["Content"].ToString();
            this.TempleId = StringHelper.isNumber_int(oldDataTable.Rows[0]["TempleId"].ToString());
            MusicBLL musicBLL = new MusicBLL();
            musicBLL.BindSelectedMusic(StringHelper.isNumber_int(oldDataTable.Rows[0]["MusicId"].ToString()), ddlMusic);
        }
    }

    public void initMusic()
    {
        if (MusicId == 0)
        {
            notSet = "display:block;";
            play = "display:none;";
        }
        else
        {
            MusicBLL musicBll = Music_Bll.CreateNew;
            MusicModel musicModel = new MusicModel();
            DataTable dataTable = musicBll.GetDataTable("id=" + MusicId);
            if (dataTable.Rows.Count > 0)
            {
                musicModel = musicBll.GetModel(dataTable.Rows[0]);
            }
            if (musicModel.MusicType == 0)
            {
                musicPath = configInfo.InstallDir + musicModel.MusicPath;
            }
            else
            {
                musicPath = musicModel.MusicPath;
            }
        }
    }

    public HappyPetModel getModel()
    {
        Users_Model userModel = Base_GetUserInfo;
        DataTable oldDataTable = new DataTable();
        if (ViewState["HappyPetInfo"] != null)
        {
            oldDataTable = ViewState["HappyPetInfo"] as DataTable;
        }

        HappyPetModel model = new HappyPetModel();
        model.PetName = txtPetName.Text;
        model.PetPhoto = Txt_UpImg.Text;
        model.Content = WebEditor1.Text;
        model.MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        if (oldDataTable.Rows.Count > 0)
        {
            model.Id = StringHelper.isNumber_int(oldDataTable.Rows[0]["Id"].ToString());
            model.IsCheck = StringHelper.isNumber_int(oldDataTable.Rows[0]["IsCheck"].ToString());
            model.IsDel = StringHelper.isNumber_int(oldDataTable.Rows[0]["IsDel"].ToString());
            model.TempleId = StringHelper.isNumber_int(oldDataTable.Rows[0]["TempleId"].ToString());
            model.AcceeCount = StringHelper.isNumber_int(oldDataTable.Rows[0]["AcceeCount"].ToString());

            model.CreateTime = DateTime.Parse(oldDataTable.Rows[0]["CreateTime"].ToString());
        }
        if (userModel.UserId > 0)
        {
            model.CreateId = userModel.UserId;
        }

        return model;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        HappyPetModel model = getModel();
        bll.UpdateHappPet(model);
        DrePoint();
        ResultStr("HappyPetList.aspx", "修改成功!");
    }

    protected void DrePoint()
    {
        int ClanTempleId = StringHelper.isNumber_int(ViewState["Id"]);
        //AncestralHall_Model ClanMd = AncestralHall_Bll.CreatNew.GetModel(ClanTempleId);
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        //int needPoint = CreateNetMuPoint(NetMuTypeEnum(model.MuType));
        int TemplatePoint = StringHelper.isNumber_int(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        //if (TempId == this.TempleId)
        //{
        //    return;
        //}
        //if (UserId != 0)
        //{
        //    if (TemplatePoint != 0)
        //    {
        //        userBll.DrePoint(TemplatePoint, UserId);
        //    }
        //}
        //else
        //{
        //    CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/SelectMemorialTemplate.aspx", "请您登录", true);
        //}


        if (TempId == this.TempleId)
        {
            Response.Write("<script>alert('保存成功!');location.href='UpdateHappyPet_" + ViewState["Id"] + ".Aspx';</script>");
            return;
        }
        if (UserId != 0)
        {
            if (TemplatePoint != 0)
            {
                if (TemplatePoint <= Base_GetUserInfo.Point)
                {

                    Temple_Model TmpMd = templateBLL.GetModel(TempId);
                    if (TmpMd.Id > 0)
                    {

                        Point_Log_Model PointMd = new Point_Log_Model();
                        PointMd.Addtime = DateTime.Now;
                        PointMd.NetIp = StringHelper.Ip();
                        PointMd.Point = TemplatePoint;
                        PointMd.Readme = "用户修改" + EnumDescription.GetFieldText(Channel_Type.FindFamily) + " 模板为" + TmpMd.TempleName + "，扣除点数" + TemplatePoint + "点";
                        PointMd.UserId = UserId;
                        PointMd.UserType = (int)UserTypeEnum.User;
                        Point_Log_Bll.CreateNew.Add(PointMd);
                    }
                    else
                    {
                        Response.Write("<script>alert('保存失败,当前模板不存在!');location.href='UpdateHappyPet_" + ViewState["Id"] + ".Aspx';</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('保存失败,用户点数不足!');location.href='UpdateHappyPet_" + ViewState["Id"] + ".Aspx';</script>");
                    return;
                }


            }
            HapppNet_BLL.CreatNew.filterUpdate(" TemplateId= " + TempId, " id=" + ViewState["Id"]);
            //NetMu_Bll.CreatNew.filterUpdate(" TemplateId= " + TempId, " id=" + ViewState["Id"]);
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/UpdateHappyPet_" + ViewState["Id"] + ".Aspx", "请您登录", true);
        }
        Response.Write("<script>alert('保存成功!');location.href='UpdateHappyPet_" + ViewState["Id"] + ".Aspx';</script>");
    }

    protected void btnMusic_Click(object sender, EventArgs e)
    {
        int MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        //if (MusicId > 0)
        //{
        bll.SetMusic(id, MusicId);
        //}
        Response.Redirect("UpdateHappyPet_" + id + ".Aspx");
    }
}
